package com.phospherus.flow.service

import com.phospherus.flow.entity.Tcftofactory
import com.phospherus.flow.repository.TcftofactoryRepository
import org.apache.commons.lang.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.cglib.beans.BeanMap
import org.springframework.stereotype.Service

/**
 * 流程3表单数据
 * @author songjinlong
 * @version 1.0
 * @since 2017/12/27
 */
@Service
class TcftofactoryService : FormInstBaseService() {
    @Autowired
    lateinit var repository: TcftofactoryRepository

    override fun getFormInstServiceName(): String = "tcftofactory"

    override fun save(map: MutableMap<String, Any>) {
        var formInstId = map["formInstId"] as Long
        var tcftofactory = createEntityByMap<Tcftofactory>(map, Tcftofactory::class.java)
        tcftofactory.formInstId = formInstId
        tcftofactory = repository.save(tcftofactory)
        if (StringUtils.isNotBlank(map["attachedFiles"] as? String)) {
            attachedFileService.addFormInstAttachedFile(map["attachedFiles"].toString(), formInstId, "tcftofactory", tcftofactory.id)
        }
    }

    override fun query(formInstId: Long): MutableMap<String, Any?> {
        var tcftofactory = repository.findByFormInstId(formInstId)
        tcftofactory.attachedFileList = attachedFileService.getFormAttachedFiles(formInstId, tcftofactory.id, "tcftofactory")
        val beanMap = BeanMap.create(tcftofactory)
        var map = mutableMapOf<String, Any?>()
        for ((k, v) in beanMap) {
            map[k as String] = v
        }
        return map
    }
}